package com.baizhi.controller;


import com.baizhi.entity.Pet;
import com.baizhi.service.IPetService;
import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huiJie
 * @since 2021-08-27
 */
@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @GetMapping
    public R showPage(Integer pageNum,Integer pageSize){
        return R.ok(petService.showPagePet(pageNum,pageSize));
    }
    @GetMapping("{id}")
    public R showOne(@PathVariable("id")Integer id){
        return R.ok(petService.getById(id));
    }
    @DeleteMapping("{id}")
    public R remove(@PathVariable("id")Integer id){
        try {
            petService.removeById(id);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @PostMapping
    public R add(@RequestBody Pet pet){
        try {
            petService.save(pet);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
    @PutMapping
    public R update(@RequestBody Pet pet){
        try {
            QueryWrapper<Pet> wrapper = new QueryWrapper<>();
            wrapper.eq("id",pet.getId());
            petService.update(pet,wrapper);
            return R.ok(null);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}

